package net.mikoo.seals.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.mina.proxy.utils.StringUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.api.model.install.ApplyInstallRequest;
import net.mikoo.seals.api.model.install.InstallVerifyDetailRequest;
import net.mikoo.seals.api.model.install.InstallVerifyDetailResponse;
import net.mikoo.seals.api.model.install.InstallVerifyListRequest;
import net.mikoo.seals.api.model.install.InstallVerifyListResponse;
import net.mikoo.seals.api.model.install.InstallVerifyRequest;
import net.mikoo.seals.api.utility.PartyUtility;
import net.mikoo.seals.biz.service.InstallServices;
import net.mikoo.seals.common.enums.InstallStatusEnum;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Install;
import net.mikoo.seals.model.InstallQuery;

@Controller
public class InstallController {

	@Autowired
	private InstallServices installServices;
	
	private static Logger logger = LoggerFactory.getLogger(InstallController.class);
	
	@RequestMapping("/install/apply")
	@ResponseBody
	public BaseResponse apply(ApplyInstallRequest request, HttpServletRequest httpRequest) {
		BaseResponse result = new BaseResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getDevicesId() == null) {
				throw new IllegalArgumentException("request.devicesId");
			}
			
			if(StringUtils.isBlank(request.getType())) {
				throw new IllegalArgumentException("request.type");
			}
			
			Install install = new Install();
			install.setDevicesId(request.getDevicesId());
			install.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			install.setPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			install.setInstallType(request.getType());
			installServices.apply(install);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/install/waitVerify")
	@ResponseBody
	public InstallVerifyListResponse waitVerify(InstallVerifyListRequest request, HttpServletRequest httpRequest) {
		
		InstallVerifyListResponse result = new InstallVerifyListResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getPageNo() == null || request.getPageNo() <= 0) {
				request.setPageNo(1);
			}
			
			if(request.getPageSize() == null || request.getPageSize() <= 0) {
				request.setPageSize(5);
			}
			
			InstallQuery query = new InstallQuery();
			query.setOffset((request.getPageNo() - 1) * request.getPageSize());
			query.setPageSize(request.getPageSize());
			query.setVerifyPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			query.setInstallStatus(InstallStatusEnum.PENDING.getCode());
			PageModel<Install> pm = installServices.getPageByQuery(query);
			
			result.setData(pm);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/install/verifyDetail")
	@ResponseBody
	public InstallVerifyDetailResponse verifyDetail(InstallVerifyDetailRequest request) {
		
		InstallVerifyDetailResponse result = new InstallVerifyDetailResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getInstallId()== null) {
				throw new IllegalArgumentException("installId");
			}
			result.setData(installServices.getInfoById(request.getInstallId()));
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/install/verify")
	@ResponseBody
	public BaseResponse verify(InstallVerifyRequest request, HttpServletRequest httpRequest) {
		BaseResponse result = new BaseResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getInstallId()== null) {
				throw new IllegalArgumentException("installId");
			}
			
			if(StringUtils.isBlank(request.getResult())) {
				throw new IllegalArgumentException("result");
			}
			Install install = new Install();
			install.setInstallId(request.getInstallId());
			install.setVerifyPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			install.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			install.setInstallStatus(request.getResult());
			installServices.verify(install);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
}
